#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>

int main()
{
	pid_t pid=-1;
	int n = 3,i=0;
	for(i = 0; i < n; i++)
	{
		pid = fork();
        if(pid < 0){
            perror("fork err");
            return -1;
        }else if(pid == 0)
		{   
		    printf("in %d child (PID:%d), parent:%d\n",i+1,getpid(),getppid());
			exit(0);
		}
	}

    while(1){
        int ret=waitpid(pid,0,WNOHANG);
        if(ret > 0){
            break;
        }else if(ret < 0){
            printf("waitpid err");
        }
        sleep(1);
    }

    printf("in parent (PID:%d),last child:%d\n",getpid(),pid);

	return 0;
}
